<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人主页</title>
    <link rel="stylesheet" href="css/mine.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<header>
    <div class="logo">
        <img src="img/logo.png" alt="购物网站标志">
    </div>
    <div class="user-info">
        欢迎，<span id="username">加载中...</span> |  <a href="javascript:logout()" class="logout">退出登录</a>

    </div>
</header>

<nav>
    <ul>
        <li><a href="home.html">首页</a></li>
        <li><a href="shoppingcart.html">购物车</a></li>
        <li>
            <a >我的订单</a>
            <ul class="dropdown">
            <li><a href="order.html#all">历史订单</a></li>
            <li><a href="order.html#unpaid">待支付</a></li>
            <li><a href="order.html#paid">已支付</a></li>
            <li><a href="order.html#uncommented">待评价</a></li>
            <li><a href="order.html#commented">已评价</a></li>
        </ul>
        </li>
        <li>
            <a >设置</a>
            <ul class="dropdown">
                <li><a href="setting.html#profile">个人信息</a></li>
                <li><a href="setting.html#change-password">修改密码</a></li>
            </ul>
        </li>
    </ul>
</nav>


<main>
    <section class="user-profile">
        <h2>个人资料</h2>
        <p>用户名: <span id="profile-username">加载中...</span></p>
        <p>邮箱: <span id="email">加载中</span></p>
        <p>手机号: <span id="phone">加载中</span></p>
        <p>收货地址: <span id="address">加载中</span></p>
        <p>注册日期: <span id="createtime">加载中</span></p>
    </section>

</main>

<footer>
    <p>&copy; 购物网站。</p>
</footer>

<script>
    $(document).ready(function() {
        // 发送AJAX请求获取用户名
        $.ajax({
            url: "/user/get-userinfo", // 假设这是获取当前用户信息的API
            type: "GET",
            dataType: "json",
            success: function(response) {
                if (response.state === 200) {
                    $('#username').text(response.data.username);
                    $('#profile-username').text(response.data.username);
                    $('#email').text(response.data.email);
                    $('#phone').text(response.data.phone);
                    $('#address').text(response.data.address);
                    $('#createtime').text(response.data.createtime);
                } else {
                    // 处理未登录或其他错误情况
                    $('#username').text('加载错误');
                    $('#profile-username').text('加载错误');
                    $('#email').text('加载错误');
                    $('#phone').text('加载错误');
                    $('#address').text('加载错误');
                    $('#createtime').text('加载错误');

                }
            },
            error: function() {
                $('#username').text('加载错误');
                $('#profile-username').text('加载错误');
                $('#email').text('加载错误');
                $('#phone').text('加载错误');
                $('#address').text('加载错误');
                $('#createtime').text('加载错误');
            }
        });
    });
    function logout() {
        if (confirm("确认要退出登录？")) {
            // 发送AJAX请求退出登录
            $.ajax({
                url: "user/logout",
                type: "get",
                success: function (response) {
                    if (response.state === 200) {
                        alert("退出成功");
                        location.href = "home.html";
                    } else {
                        alert("当前未登陆，点击确认回到首页");
                        location.href = "home.html";
                    }
                },
                error: function () {
                    // 说明当前退出的状态不正确，可能未登录，强行跳转到登录页面.
                    alert("抱歉，当前状态异常，请点击确认！")
                    location.assign('/home.html');
                }
            });
        }
    }

    // 检查用户是否登录
    function checkLogin() {
        $.ajax({
            url:"/user/get-user",
            type:"get",
            success: function(result) {
                if (result.state == 401) {
                    alert("登陆已失效，请重新登陆");
                    location.href = "home.html";
                }
            },
            error: function(err) {
                if (err.status == 401) {
                    alert("当前用户未登录，你即将回到首页");
                    location.href = "home.html";
                }
            }
        })
    }
    checkLogin();

</script>
</body>
</html>